package com.bw.ivideoproject.home

import android.annotation.SuppressLint
import android.util.Log
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.bw.ivideoproject.App
import com.bw.ivideoproject.db.database.HistoryDatabase
import com.bw.ivideoproject.db.entity.HistoryEntity
import com.bw.ivideoproject.entity.CommentEntity
import com.bw.ivideoproject.entity.SimpleVideoEntity
import com.bw.ivideoproject.entity.SimpleVideoTypeEntity
import com.bw.lib_mvvm.BaseViewModel
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

class HomeViewModel : BaseViewModel() {

    private val model:HomeModel by lazy {
        getModel(HomeModel::class.java)
    }
    val db: HistoryDatabase by lazy {
        App.getDatabase()
    }

    val typeLiveData = MutableLiveData<List<SimpleVideoTypeEntity>>()
    val videoLiveData = MutableLiveData<List<SimpleVideoEntity>>()
    val keywordLiveData = MutableLiveData<List<SimpleVideoEntity>>()
    val recommendLiveData = MutableLiveData<List<SimpleVideoEntity>>()
    val commentLiveData = MutableLiveData<List<CommentEntity>>()
    val sendLiveData = MutableLiveData<CommentEntity>()
    val searchLiveData = MutableLiveData<Array<String>>()
    val filedLiveData = MutableLiveData<String>()


    fun getSimpleVideoType(){
        viewModelScope.launch {
            val (code,data,message) = model.getSimpleVideoType()
            if(code == 0){
                typeLiveData.value = data
            }else{
                filedLiveData.value = message
            }
        }
    }

    fun getSimpleVideo(channelId:String,page:Int,pageSize:Int){
        viewModelScope.launch {
            val (code,data,message) = model.getSimpleVideo(channelId, page, pageSize)
            if(code == 0){
                videoLiveData.value = data
            }else{
                filedLiveData.value = message
            }
        }
    }

    fun getRecommendVideo(page:Int,pageSize:Int){
        viewModelScope.launch {
            val (code,data,message) = model.getRecommendVideo( page, pageSize)
            if(code == 0){
                recommendLiveData.value = data
            }else{
                filedLiveData.value = message
            }
        }
    }

    fun getComment(dataType:Int,itemId:String){
        viewModelScope.launch {
            val (code,data,message) = model.getComment(dataType, itemId)
            if(code == 0){
                commentLiveData.value = data
            }else{
                filedLiveData.value = message
            }
        }
    }

    fun sendComment(entity: CommentEntity){
        viewModelScope.launch {
            val (code,data,message) = model.sendComment(entity)
            if(code == 0){
                sendLiveData.value = data
            }else{
                filedLiveData.value = message
            }
        }
    }

    fun getHistorySearch(){
        viewModelScope.launch {
            withContext(Dispatchers.IO){
                val historySearch = db.getHistoryDao().getAll()
                    withContext(Dispatchers.Main){
                        if(historySearch.isNotEmpty()){
                            val arrayOf = Array(historySearch.size) { i -> historySearch[i].keyword }
                            searchLiveData.value = arrayOf
                        }else{
                            filedLiveData.value = "暂无搜索记录"
                        }
                    }
            }

        }
    }

    fun insertSearch(entity: HistoryEntity){
        viewModelScope.launch {
            withContext(Dispatchers.IO){
                db.getHistoryDao().insert(entity)
            }
        }
    }

    fun getVideoByKeyword(keyword: String,page:Int,pageSize: Int){
        viewModelScope.launch {
            val (code,data,msg) = model.getVideoByKeyword(keyword, page, pageSize)
            if(code == 0){
                keywordLiveData.value = data
            }else{
                filedLiveData.value = msg
            }
        }
    }
}